﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BizCollegeWebApp.Models;
using System.IO;
using System.Net.Mime;


namespace BizCollegeWebApp.Controllers
{
    public class ArticleController : Controller
    {
        //
        // GET: /Article/

        BizCollegeDBDataContext db = new BizCollegeDBDataContext();

        public ActionResult Index(string SearchTerm)
        {
            // Temp:
            

            if (string.IsNullOrEmpty(SearchTerm))
            {
                ViewBag.SearchTerm = string.Empty;
            }
            else
            {
                ViewBag.SearchTerm = SearchTerm;
            }

            return View(this.db.GetArticles(SearchTerm).AsEnumerable());
        }

        public ActionResult Details(int id)
        {
            Article ac = this.db.Articles.FirstOrDefault(cc => cc.ArticleID == id);
            if (ac == null)
            {
                return RedirectToAction("Details");
            }

            return View(ac);
        }

        //
        // GET: /Article/Create

        public ActionResult Create()
        {

            Article article = new Article();
            return View("Create", article);
        }

        //
        // POST: /Article/Create

        [HttpPost]
        public ActionResult Create(Article article)
        {
            try
            {
                if (ModelState.IsValid)
                {

                    article.CreatedBy = Current.User.Username;
                    article.CreatedDtTm = System.DateTime.Now;
                    article.LastUpdatedBy = Current.User.Username;
                    article.UpdatedDtTm = System.DateTime.Now;

                    this.db.Articles.InsertOnSubmit(article);
                    this.db.SubmitChanges();

                    return RedirectToAction("Index");
                }
                return View(article);
            }
            catch
            {
                return View(article);
            }
        }

        // GET: /Article/Edit/5

        [HttpGet]
        public ActionResult Edit(int id)
        {
            Article c = this.db.Articles.FirstOrDefault(cc => cc.ArticleID == id);
            if (c == null)
            {
                return RedirectToAction("Index");
            }
            return View(c);
        }

        [HttpPost]
        public ActionResult Edit(int id, Article viewArticle)
        {
            Article c = this.db.Articles.FirstOrDefault(cc => cc.ArticleID == id);
            if (c == null)
            {
                return RedirectToAction("Index");
            }
            try
            {
                if (ModelState.IsValid)
                {
                    c.ArticleName = viewArticle.ArticleName;
                    c.ArticleDescription = viewArticle.ArticleDescription;
                    c.LastUpdatedBy = Current.User.Username;
                    c.UpdatedDtTm = System.DateTime.Now;

                    this.db.SubmitChanges();

                    return RedirectToAction("Index");
                }
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("ex", ex);
            }
            return View(c);
        }

        //// POST: /Article/Delete/5

        public ActionResult Delete(int id)
        {
            BizCollegeWebApp.Models.Article article = this.db.Articles.FirstOrDefault(cc => cc.ArticleID == id);
            if (article != null)
            {
                this.db.Articles.DeleteOnSubmit(article);
                this.db.SubmitChanges();
            }
            return RedirectToAction("Index");
        }
    }
}
